package com.zxhhyj.music.logic.table

import com.zxhhyj.storm.schema.Table
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import kotlin.time.Clock
import kotlin.time.ExperimentalTime

sealed interface MediaSource {
    val id: Int?
    val createDatetime: LocalDateTime?

    data class Webdav(
        override val id: Int? = null,
        override val createDatetime: LocalDateTime? = null,
        val username: String,
        val password: String,
    ) : MediaSource

    data class FTP(
        override val id: Int? = null,
        override val createDatetime: LocalDateTime? = null,
        val username: String,
        val password: String
    ) : MediaSource
}

object WebdavSourceTable : Table<MediaSource.Webdav>("t_webdav_source") {
    val id = int("id").primaryKey(autoincrement = true).nullable().bindTo(MediaSource.Webdav::id)

    val username = varchar("username").bindTo(MediaSource.Webdav::username)

    val password = varchar("password").bindTo(MediaSource.Webdav::password)

    @OptIn(ExperimentalTime::class)
    val createDatetime = datetime("create_datetime")
        .default { Clock.System.now().toLocalDateTime(TimeZone.UTC) }
        .bindTo(MediaSource.Webdav::createDatetime)
}